package Servlets.lhl;/**
 * 项目名称：SaleOderSerchServlet.java
 * 文件名称：SaleOderSerchServlet.java
 * 类的描述：无
 * 创建时间：2025-07-26 15:04:13
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import Bean.sales_order;
import Util.GokJDBCUtil2;
import Util.RES;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "SaleOderSerchServlet", value = "/sos")
public class SaleOderSerchServlet extends HttpServlet {

    public void init() {
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
//        username=2826894957&paymentStatus=0&orderStatus=1&startDate=2025-07-20&endDate=2025-07-27
//        String page = req.getParameter("page");
//        if(page == null || page.equals("")){
//            res.getWriter().write(RES.fail("参数错误"));
//            return;
//        }
//        int i = Integer.parseInt(page);
        String username = req.getParameter("username");
        String paymentStatus = req.getParameter("paymentStatus");
        String orderStatus = req.getParameter("orderStatus");
        String startDate = req.getParameter("startDate");
        String endDate = req.getParameter("endDate");
        StringBuilder sql = new StringBuilder(
                "SELECT id, code, userid, ordertime, totalamount, paymentstatus, orderstatus, remark, isdeleted,"
                        + "(SELECT username FROM user WHERE userid = user.id) AS username "
                        + "FROM sales_order WHERE 1=1"
        );
        List<String> params = new ArrayList<>();  // 用于存储条件值（实际开发中应使用PreparedStatement）
        // 用户名条件（使用子查询进行精确匹配）
        if (username != null && !username.trim().isEmpty()) {
            sql.append(" AND EXISTS (SELECT 1 FROM user WHERE user.id = sales_order.userid AND username like ?)");
            params.add("%"+username.trim()+"%");
        }
        // 支付状态条件
        if (paymentStatus != null && !paymentStatus.trim().isEmpty()) {
            sql.append(" AND paymentstatus = ?");
            params.add(paymentStatus.trim());
        }
        // 订单状态条件
        if (orderStatus != null && !orderStatus.trim().isEmpty()) {
            sql.append(" AND orderstatus = ?");
            params.add(orderStatus.trim());
        }
        // 订单时间范围（开始日期）
        if (startDate != null && !startDate.trim().isEmpty()) {
            sql.append(" AND ordertime >= ?");
            params.add(startDate.trim() + " 00:00:00");
        }
        // 订单时间范围（结束日期）
        if (endDate != null && !endDate.trim().isEmpty()) {
            sql.append(" AND ordertime <= ?");
            params.add(endDate.trim() + " 23:59:59");
        }
//        sql.append(" limit ?,? ");
//        params.add(String.valueOf((i-1)*10));
//        params.add("10");
        // 添加删除状态过滤（可选）
        sql.append(" AND isdeleted = 0");
        List<sales_order> salesOrders = GokJDBCUtil2.queryAll(sql.toString(), sales_order.class, params.toArray());
        req.setAttribute("salesOrders", salesOrders);
//        req.setAttribute("startDate1", startDate);
//        req.setAttribute("endDate1", endDate);
//        req.setAttribute("username1",username);
//        req.setAttribute("paymentStatus1",paymentStatus);
//        req.setAttribute("orderStatus1",orderStatus);
//        req.setAttribute("page", page);
//        req.setAttribute("count",salesOrders.size());
        req.getRequestDispatcher("pages/sellOrder_manage.jsp").forward(req,res);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("GET")) {
                this.doGet(req, res);
            } else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}